package com.caijinbang.study.utils;

import org.springframework.data.mongodb.core.query.Query;

/**
 * MongoDB工具类
 *
 * @author caijb
 * @version 1.0.0
 * @date 2018-05-03 12:30
 */
public class MongoDbUtils {

    public static final String MONGO_LIKE = "^.*value.*$";

    /**
     * 添加查询关键字
     *
     * @param query       查询条件
     * @param includeKeys 包含关键字
     */
    public static void addIncludeKeys(Query query, String[] includeKeys) {
        if (null != includeKeys && includeKeys.length > 0) {
            for (String key : includeKeys) {
                query.fields().include(key);
            }
        }
    }

    /**
     * like查询
     *
     * @param value
     * @return
     */
    public static String toLike(String value) {
        if (value == null) {
            return null;
        }
        return MongoDbUtils.MONGO_LIKE.replace("value", value);
    }
}
